package com.luckylo.activemq.local;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 发布/订阅 模式
 *
 * @user: luckylo
 * @date: 2018-1-18
 */
public class TopicModel {

    private static Session session;

    static {
        try {
            init();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    private static void init() throws JMSException {
        // 获取连接工厂
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();

        // 获取连接
        Connection connection = factory.createConnection();

        // 启动.才能获取session
        connection.start();

        // 参数未搞明白意思
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    }

    public static void main(String[] args) throws JMSException {
        // pub-sub 模式,发布订阅
        Topic topic = session.createTopic("topic");

        MessageProducer producer = session.createProducer(topic);
        producer.send(session.createTextMessage("topic model test"));
    }

}
